package nl.hanze.designpatterns.mvc.model;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import nl.hanze.designpatterns.DAO.TroubleTicketDAO;
import nl.hanze.designpatterns.DAO.impl.db.TroubleTicketDAOImpl;
import nl.hanze.designpatterns.domain.TroubleTicket;
import nl.hanze.designpatterns.mvc.application.Application;
import nl.hanze.designpatterns.mvc.view.TroubleTicketView;

public class TroubleTicketModel extends Model {

	private TroubleTicketDAO DAO;
	private List<TroubleTicket> tickets;

	public TroubleTicketModel(){
		DAO = new TroubleTicketDAOImpl();
	}
	
	public void addTroubleTicket(String caller, String description){
		TroubleTicket ticket = new TroubleTicket();
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		ticket.setDateTime(format.format(new Date()));
		ticket.setUserName(Application.getUserName());
		ticket.setCallerName(caller);
		ticket.setDescription(description);
		
		DAO.saveTroubleTicket(ticket);
	}
	
	
	
	public List<TroubleTicket> getTroubleTicketByName(String callerName){
		this.tickets = DAO.getTroubleTicketsByName(callerName);
		return tickets;
	}
	
	public List<TroubleTicket> getAllTroubleTickets(){
		this.tickets = DAO.getAllTroubleTickets();
		return tickets;
	}
	
	public String[] getCallers(){
		return DAO.getAllCallers();
	}
	
	public void getSelectTicket(int row){
		if(row >= 0){
			TroubleTicketView view = (TroubleTicketView) getView("troubeTicketOverview");
			if(view != null){
				view.updateSelectedView(this.tickets.get(row));
			}else{
				System.err.println("view is null");
			}
			
			
		}
		
	}
	
	public Object[][] convertTroubleTicketsToArray(List<TroubleTicket> tickets){
		Object[][] object = new Object[tickets.size()][4];
		for(int i = 0; i < tickets.size(); i++){
			object[i][0] = tickets.get(i).getUserName();
			object[i][1] = tickets.get(i).getCallerName();
			object[i][2] = tickets.get(i).getDescription();
			object[i][3] = tickets.get(i).getDateTime();
		}
		return object;
	}
}
